package top.sssd.auth.controller.rolePermission;


import java.time.LocalDateTime;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import top.sssd.auth.common.CommonResponse;
import top.sssd.auth.model.rolePermission.request.RolePermissionQueryReq;
import top.sssd.auth.model.rolePermission.request.RolePermissionSaveOrUpdateReq;
import top.sssd.auth.model.rolePermission.response.RolePermissionResp;
import top.sssd.auth.service.rolePermission.RolePermissionService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;


@Api(tags = "角色权限关联表管理")
@RestController
@RequestMapping("/rolepermission")
public class RolePermissionController {

    @Resource
    private RolePermissionService rolePermissionService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询角色权限关联表", httpMethod = "GET")
    public CommonResponse<RolePermissionResp> getById(@PathVariable Serializable id) {
        return CommonResponse.ok(rolePermissionService.getRolePermissionRespById(id));
    }

    @GetMapping
    @ApiOperation(value = "分页条件查询角色权限关联表", httpMethod = "GET")
    public CommonResponse<Page<RolePermissionResp>> page(RolePermissionQueryReq rolePermissionQueryReq) {
        return CommonResponse.ok(rolePermissionService.queryPage(rolePermissionQueryReq));
    }

    @PostMapping
    @ApiOperation(value = "新增一条数据到角色权限关联表", httpMethod = "POST")
    public CommonResponse<Boolean> save(@RequestBody RolePermissionSaveOrUpdateReq rolePermissionSaveOrUpdateReq) {
        Boolean successFlag = rolePermissionService.saveRolePermissionReq(rolePermissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PutMapping
    @ApiOperation(value = "更新一条数据到角色权限关联表", httpMethod = "PUT")
    public CommonResponse<Boolean> update(@RequestBody RolePermissionSaveOrUpdateReq rolePermissionSaveOrUpdateReq) {
        Boolean successFlag = rolePermissionService.updateRolePermissionReq(rolePermissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @PatchMapping
    @ApiOperation(value = "更新部分数据到角色权限关联表", httpMethod = "PATCH")
    public CommonResponse<Boolean> patch(@RequestBody RolePermissionSaveOrUpdateReq rolePermissionSaveOrUpdateReq) {
        Boolean successFlag = rolePermissionService.updateRolePermissionReq(rolePermissionSaveOrUpdateReq);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据多个ID删除角色权限关联表", httpMethod = "DELETE")
    public CommonResponse<Boolean> delete(@PathVariable List<Long> ids) {
        Boolean successFlag = rolePermissionService.removeByIds(ids);
        return CommonResponse.ok(successFlag, successFlag ? "success" : "fail");
    }
}
